public class ArrayQueue {
    int front;
    int rear;
    int capacity;
    int array[];

    public ArrayQueue(int size){
        front = -1;
        rear = -1;
        capacity = size;
        array = new int[size];
    }

    /**
     * 创建队列
     * @param size:队列大小
     * @return
     */
    static ArrayQueue createArray(int size){
        return new ArrayQueue(size);
    }

    /**
     * 判断队列是否为空
     * @return
     */
    boolean isEmpty(){
        if(front == -1 && rear == -1){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 判断队列是否为满
     * @return
     */
    boolean isFull(){
        return ((rear+1) % capacity == front);
    }

    /**
     * 判断队列大小
     * @return 返回队列长度
     */
    int getQueueSize(){
        return ((capacity - rear + front + 1) % capacity);
    }

    /**
     * 在队尾插入一个数据
     * @param data:插入的数据
     */
    void enQueue(int data){
        if(isFull()){
            System.out.println("队列已满");
        }else{
            rear = (rear + 1) % capacity;
            array[rear] = data;
            //队列为空
            if(front == -1){
                front = rear;
            }
        }
    }

    /**
     * 删除并返回队列头元素
     * @return
     */
    int deQueue(){
        int data = 0;
        if(isEmpty()){
            System.out.println("队列为空");
            return 0;
        }else{
            data = array[front];
            if(front == rear){  //队列为满
                front = rear - 1;
            }else{
                front = (front + 1) % capacity;
            }
            return data;
        }
    }
}

class ArrayQueueTest{
    public static void main(String[] args) {
        ArrayQueue arrayQueue = new ArrayQueue(5);
        arrayQueue.enQueue(2);
        arrayQueue.enQueue(4);
        arrayQueue.enQueue(3);
        arrayQueue.enQueue(5);
        //arrayQueue.enQueue(5);
        arrayQueue.deQueue();
        arrayQueue.deQueue();
        for(int i = 0;i < arrayQueue.getQueueSize(); i++){
            System.out.println(arrayQueue.array[i]);
        }

    }
}